package view.cloudFactory;

import com.jfoenix.controls.JFXCheckBox;
import com.jfoenix.controls.JFXComboBox;
import com.jfoenix.controls.JFXTextField;
import controller.EquipMsgController;
import dao.EquipMsgDao;
import dao.EquipTypeDao;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.control.Alert;
import pojo.equip.EquipMsgEntity;
import pojo.equip.EquipTypeEntity;
import view.Main;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @author Limohan
 */
public class ChangeFactoryEquipController {
    private Main myApp;
    private final SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
    private static String  id;
    private static String factory;

    public void setApp(Main myApp) {
        this.myApp = myApp;
    }

    public void setId(String id) {
        this.id = id;
    }

    public  String getFactory() {
        return factory;
    }

    public  void setFactory(String factory) {
        this.factory = factory;
    }

    public ChangeFactoryEquipController() {
    }

    @FXML
    private JFXTextField name;

    @FXML
    private JFXTextField mode;

    @FXML
    private JFXTextField detail;

    @FXML
    private JFXComboBox<String> type;

    @FXML
    private JFXCheckBox isFactoryEquip;

    @FXML
    void initialize() throws IOException {
        EquipTypeDao equipTypeDao = new EquipTypeDao();
        for (EquipTypeEntity equipTypeEntity : equipTypeDao.findTypeList()) {
            type.getItems().add(equipTypeEntity.getName());
        }
    }

    @FXML
    void returnMsgMangePage(ActionEvent event) throws IOException {
        myApp.goToCloudFactoryManagePage();
    }

    @FXML
    void saveNewEquip(ActionEvent event) throws IOException {
        EquipMsgDao equipMsgDao = new EquipMsgDao();
        equipMsgDao.deleteMsg(id);
        EquipMsgEntity equipMsgEntity = new EquipMsgEntity();
        EquipMsgController equipMsgController = EquipMsgController.getInstance();
        Date date = new Date();
        equipMsgEntity.setCode("PD"+sdf.format(date));
        equipMsgEntity.setDetail(detail.getText());
        equipMsgEntity.setMode(mode.getText());
        equipMsgEntity.setType(type.getValue());
        equipMsgEntity.setName(name.getText());
        equipMsgEntity.setRentState("未被租用");
        if(isFactoryEquip.isSelected()){
            equipMsgEntity.setSource("自有设备");
            equipMsgEntity.setRentState("工厂设备");
        }else{
            equipMsgEntity.setSource("租用设备");
            equipMsgEntity.setRentState("未被租用");
        }
        equipMsgEntity.setEquipState("已关闭");

        Alert alert = new Alert(Alert.AlertType.INFORMATION);
        alert.setHeaderText("!");
        alert.setContentText(equipMsgController.addMsg(equipMsgEntity));
        alert.showAndWait();

    }

    public void originDisplay() throws IOException {
        EquipMsgDao equipMsgDao = new EquipMsgDao();
        EquipMsgEntity equipMsgEntity = equipMsgDao.findMsg(id);
        name.setText(equipMsgEntity.getName());
        type.setValue(equipMsgEntity.getType());
        mode.setText(equipMsgEntity.getMode());
        detail.setText(equipMsgEntity.getDetail());
        if(equipMsgEntity.getSource()!=null){
            if(equipMsgEntity.getSource().equals("自有设备")){
                isFactoryEquip.setSelected(true);
            }
        }

        }

    }

